package com.mep.auth.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.mep.auth.constant.SysConstants;
import com.mep.auth.feign.IFeignSysUserApi;
import com.mep.auth.model.SysUser;
import com.mep.auth.service.SysUserService;
import com.mep.common.http.HttpResult;
import com.mep.common.page.PageRequest;
import com.mep.common.utils.FileUtils;
import com.mep.common.utils.PasswordUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户控制器
 * @author lyl
 * @date Jan 13, 2019
 */
@Api(value="001-user的controller",tags="001-user的controller")
@RestController
@RequestMapping(value = "/user")
public class SysUserController implements IFeignSysUserApi {

	@Autowired
	private SysUserService sysUserService;
	
	@Override
	@ApiOperation(value="01.保存用户信息",notes="问题点:保存用户信息")
	@ApiOperationSupport(order = 1, author = "lyl")
//	@PostMapping(value="/save")
	public HttpResult save(@RequestBody SysUser record) {
		SysUser user = sysUserService.findById(record.getId());
		if(user != null) {
			if(SysConstants.ADMIN.equalsIgnoreCase(user.getName())) {
				return HttpResult.error("超级管理员不允许修改!");
			}
		}
		if(record.getPassword() != null) {
			String salt = PasswordUtils.getSalt();
			if(user == null) {
				// 新增用户
				if(sysUserService.findByName(record.getName()) != null) {
					return HttpResult.error("用户名已存在!");
				}
				String password = PasswordUtils.encode(record.getPassword(), salt);
				record.setSalt(salt);
				record.setPassword(password);
			} else {
				// 修改用户, 且修改了密码
				if(!record.getPassword().equals(user.getPassword())) {
					String password = PasswordUtils.encode(record.getPassword(), salt);
					record.setSalt(salt);
					record.setPassword(password);
				}
			}
		}
		return HttpResult.ok(sysUserService.save(record));
	}

	@Override
	@ApiOperation(value="02.删除用户信息",notes="注意问题点:删除用户信息")
	@ApiOperationSupport(order = 2, author = "lyl")
//	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<SysUser> records) {
		for(SysUser record:records) {
			SysUser sysUser = sysUserService.findById(record.getId());
			if(sysUser != null && SysConstants.ADMIN.equalsIgnoreCase(sysUser.getName())) {
				return HttpResult.error("超级管理员不允许删除!");
			}
		}
		return HttpResult.ok(sysUserService.delete(records));
	}
	@Override
	@ApiOperation(value="03.通过名称查询用户信息",notes="注意问题点:通过名称查询用户信息")
	@ApiOperationSupport(order = 3, author = "lyl")
//	@PostMapping(value="/findByName")
	public HttpResult findByName(@RequestParam String name) {
		return HttpResult.ok(sysUserService.findByName(name));
	}

	@Override
	@ApiOperation(value="04.通过名称查找用户权限",notes="注意问题点:通过名称查找用户权限")
	@ApiOperationSupport(order = 4, author = "lyl")
//	@PostMapping(value="/findPermissions")
	public HttpResult findPermissions(@RequestParam String name) {
		return HttpResult.ok(sysUserService.findPermissions(name));
	}
	
	@Override
	@ApiOperation(value="05.通过用户Id查询用户角色",notes="注意问题点:通过用户Id查询用户角色")
	@ApiOperationSupport(order = 5, author = "lyl")
//	@PostMapping(value="/findUserRoles")
	public HttpResult findUserRoles(@RequestParam Long userId) {
		return HttpResult.ok(sysUserService.findUserRoles(userId));
	}

	@Override
	@ApiOperation(value="06.查询用户分页数据",notes="注意问题点:查询用户分页数据")
	@ApiOperationSupport(order = 6, author = "lyl")
//	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(sysUserService.findPage(pageRequest));
	}

	@Override
	@ApiOperation(value="07.下载用户Excel的信息",notes="注意问题点:下载用户Excel的信息")
	@ApiOperationSupport(order = 7, author = "lyl")
//	@PostMapping(value="/exportExcelUser")
	public void exportExcelUser(@RequestBody PageRequest pageRequest, HttpServletResponse res) {
		File file = sysUserService.createUserExcelFile(pageRequest);
		FileUtils.downloadFile(res, file, file.getName());
	}
	
}
